package ru.jetsoft.reg112.desktop.control;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.Text;
import ru.jetsoft.reg112.desktop.DataFormatException;
import ru.jetsoft.reg112.desktop.data.DataSupplyManager;
import ru.jetsoft.reg112.desktop.data.Param;

public class MultiContainer extends ControlContainer {

    private final List<ParamControl> controls;
    private final GridPane grid;

    public MultiContainer(Param param) {
        super(param);
        grid = new GridPane();
        grid.setHgap(5);
        grid.setVgap(5);
        controls = new LinkedList<>();
        refresh();
    }

    private void refresh() {
        grid.getChildren().clear();
        Button addBtn = new Button("+");
        addBtn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                try {
                    addControl();
                } catch (DataFormatException ex) {
                }
            }
        });
        if(controls.isEmpty()) {
            Text multiText = new Text(getParam().getName());
            multiText.setFont(Font.font("System", FontPosture.ITALIC, 13));
            grid.add(multiText, 1, 1);
            grid.add(addBtn, 2, 1);
        } else {
            Button removeBtn;
            int row = 1;
            for(ParamControl control: controls) {
                final int i = row - 1;
                grid.add(control.getNode(), 1, row);
                removeBtn = new Button("x");
                removeBtn.setOnAction(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent e) {
                        removeControl(i);
                    }
                });
                grid.add(removeBtn, 2, row);
                if(row == 1) {
                    grid.add(addBtn, 3, row);
                }
                row++;
            }
        }
    }

    public List<ParamControl> getControls() {
        return controls;
    }

    public void setControls(Collection<ParamControl> controls) {
        this.controls.clear();
        this.controls.addAll(controls);
        refresh();
    }

    public void addControls(Collection<ParamControl> controls) {
        this.controls.addAll(controls);
        refresh();
    }

    public void addControl(ParamControl control) {
        this.controls.add(control);
        refresh();
    }

    public void addControl() throws DataFormatException {
        ParamControl control = DataSupplyPane.createControl(getParam(),
                getParam().getDefaultValue(),
                DataSupplyManager.generateByParams(getParam().getChilds()));
        this.controls.add(control);
        refresh();
    }

    private void removeControl(int i) {
        controls.remove(i);
        refresh();
    }

    @Override
    public Node getNode() {
        return grid;
    }
}
